import { defineStore } from 'pinia'
import { ref } from 'vue'
import { login } from '@/api/user'
import type { loginForm } from '@/api/user/type'
export const useUserStore = defineStore(
  'user',
  () => {
    //创建token存储
    const token = ref(localStorage.getItem('token'))
    //封装登录actions
    const userLogin = async (data: loginForm) => {
      const res = await login(data)
      if (res.code !== 200) {
        return Promise.reject(new Error(res.data.message))
      } else {
        token.value = res.data.token
        console.log(token.value)
        return 'ok'
      }
    }
    return {
      token,
      userLogin
    }
  },
  {
    persist: true
  }
)
